package com.lzj.admin.controller;

import com.lzj.admin.entity.DataJson;
import com.lzj.admin.utils.UploadUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Controller
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("image")
    @ResponseBody
    public DataJson uploadImage(@RequestParam(value = "file") MultipartFile multipartFile){
        // 调用我们写的上传文件的工具类
        String imagePath = UploadUtils.upload(multipartFile);
        DataJson dataJson = new DataJson();
        if(imagePath != null){
            dataJson.setCode(1);
            dataJson.setMsg("图片上传成功！");
            HashMap<String,String> map = new HashMap<>();
            map.put("src",imagePath);
            dataJson.setData(map);
        } else {
            dataJson.setCode(0);
            dataJson.setMsg("对不起，图片上传失败！");
        }
        return dataJson;
    }

}
